package com.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.annotation.ColorRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.rongshu.base.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 底部弹出的列表选择器
 */
public class BottomListDialog extends Dialog {
    private RecyclerView function_recyclerview;
    private Button btn_cancle;


    public BottomListDialog(Context context) {
        super(context, R.style.custom_dialog2);
        setContentView(R.layout.fragment_gender_selecter_dialog_view);
        initView();
    }

    private void initView() {
        function_recyclerview = findViewById(R.id.function_recyclerview);
        btn_cancle = findViewById(R.id.btn_cancle);
        btn_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        Window window = getWindow();
        window.setWindowAnimations(R.style.picker_view_slide_anim);
        WindowManager.LayoutParams layoutParams = this.getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        setCanceledOnTouchOutside(true);
    }

    public static class Builder implements View.OnClickListener {
        private BottomListDialog dialog;
        private Context mContext;
        //功能集合
        private List<String> functions = new ArrayList<>();
        //view 动画style
        //底部button数据
        private String cancle;
        @ColorRes
        private int cancle_color;
        @ColorRes
        private int option_color;
        //dialog消失监听
        private DialogInterface.OnDismissListener dismissListener;
        private GenderSelecterAdapter genderSelecterAdapter = null;
        private OnItemClickListener mOnItemClickListener;


        public Builder(Context mContext) {
            this.mContext = mContext;
            this.cancle_color = R.color.pickerview_timebtn_nor;
            this.option_color = R.color.pickerview_timebtn_nor;
        }

        public Builder setFunctions(List<String> functions) {
            this.functions = functions;
            return this;
        }

        public Builder setCancle(String cancle) {
            this.cancle = cancle;
            return this;
        }

        public Builder setCancle_color(@ColorRes int color) {
            this.cancle_color = color;
            return this;
        }

        public Builder setOption_color(@ColorRes int color) {
            this.option_color = color;
            return this;
        }

        public Builder setOnItemClickListener(OnItemClickListener listener) {
            mOnItemClickListener = listener;
            return this;
        }

        public interface OnItemClickListener {
            void onItemClick(int position, Dialog dialog);
        }

        /**
         * dialog消失监听
         */
        public Builder setDialogDimissListener(DialogInterface.OnDismissListener dismissListener) {
            this.dismissListener = dismissListener;
            return this;
        }

        public BottomListDialog create() {
            if (dialog == null) {
                dialog = new BottomListDialog(mContext);
            }
            dialog.btn_cancle.setText(cancle);
            dialog.btn_cancle.setTextColor(mContext.getResources().getColor(option_color));

            LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(mContext);

            genderSelecterAdapter = new GenderSelecterAdapter(functions);
            genderSelecterAdapter.setOption_Color(option_color);

            dialog.function_recyclerview.setLayoutManager(mLinearLayoutManager);
            dialog.function_recyclerview.setAdapter(genderSelecterAdapter);
            dialog.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    if (dismissListener != null) {
                        dismissListener.onDismiss(dialog);
                    }
                }
            });
            return dialog;
        }

        @Override
        public void onClick(View v) {

        }

        public class GenderSelecterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

            private List<String> functions;
            @ColorRes
            private int option_color;

            public GenderSelecterAdapter(List<String> functions) {
                this.functions = functions;
            }

            public void setOption_Color(@ColorRes int color) {
                this.option_color = color;
            }

            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_gender_selecter_item, parent, false);
                return new GenderSelecterViewHolder(view);
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
                GenderSelecterViewHolder genderSelecterViewHolder = (GenderSelecterViewHolder) holder;
                String option = functions.get(position);
                genderSelecterViewHolder.txt_option.setTextColor(option.equals("删除") ?
                        Color.RED : mContext.getResources().getColor(option_color));
                genderSelecterViewHolder.txt_option.setText(option);
                if (position == functions.size() - 1) {
                    genderSelecterViewHolder.line.setVisibility(View.GONE);
                }

                genderSelecterViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemClickListener != null) {
                            mOnItemClickListener.onItemClick(position, dialog);
                        }
                    }
                });
            }

            @Override
            public int getItemCount() {
                return functions.size();
            }

            public class GenderSelecterViewHolder extends RecyclerView.ViewHolder {

                public TextView txt_option;
                public View line;

                public GenderSelecterViewHolder(View itemView) {
                    super(itemView);
                    txt_option = itemView.findViewById(R.id.txt_option);
                    line = itemView.findViewById(R.id.line);
                }
            }
        }
    }
}
